﻿/**
 * @file fwave_wpcm_test.cpp
 * Copyright (c) Gaaagaa. All rights reserved.
 * 
 * @author  : Gaaagaa
 * @date    : 2020-02-17
 * @version : 1.0.0.0
 * @brief   : 录制 wave-pcm 格式的音频文件（测试 fwave_wpcm_t）。
 */

#include "fwave_pcm.h"
#include "audio_dev.h"

#define SDL_main_h_
#include <SDL/SDL.h>

#include <vector>
#include <utility>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

////////////////////////////////////////////////////////////////////////////////

int main(int argc, char * argv[])
{
    x_errno_t xerr_no = X_ERR_UNKNOW;
    x_int32_t xit_err = X_ERR_UNKNOW;

    //======================================

    if (argc < 2)
    {
        printf("Usage: \n\t %s < wave-pcm file path >\n", argv[0]);
        return -1;
    }

    SDL_Init(SDL_INIT_AUDIO);

    //======================================

    x_uint32_t xut_frequency = 48000;
    x_uint16_t xut_sampbits  = 16;
    x_uint8_t  xut_channels  = 2;

    fwave_wpcm_t   fwave_wpcm;
    adev_capture_t audio_capture;

    //======================================

    xerr_no = fwave_wpcm.open(
                            argv[1],
                            xut_frequency,
                            xut_sampbits,
                            xut_channels);
    if (XERR_FAILED(xerr_no))
    {
        printf("fwave_wpcm.open([%s]) return error code: %s\n",
               argv[1], fwave_wpcm_t::xerrno_name(xerr_no));
        return -1;
    }

    //======================================

    typedef std::vector< x_byte_t > xbuffer_t;
    typedef std::pair< fwave_wpcm_t &, xbuffer_t & > xpair_handle_t;

    xbuffer_t      xbuf;
    xpair_handle_t xpair_ctxt(fwave_wpcm, xbuf);

    audio_capture.set_callback(
        [](x_handle_t xht_ctxt,
           x_void_t * xmt_data,
           x_int32_t  xit_size) -> x_void_t
        {
            xpair_handle_t & xpair = *(xpair_handle_t *)xht_ctxt;

            if ((x_int32_t)xpair.second.size() < xit_size)
            {
                xpair.second.resize(xit_size);
            }

            if (xpair.first.is_open())
            {
                memset(xpair.second.data(), 0, xit_size);
                audio_dev_t::mix_copy(
                                    xpair.second.data(),
                                    (x_byte_t *)xmt_data,
                                    X_AUDIO_S16,
                                    xit_size);

                xpair.first.write(xpair.second.data(), xit_size);
            }
        },
        (x_handle_t)&xpair_ctxt);

    //======================================

    xit_err = audio_capture.open(
                    -1,
                    xut_channels,
                    X_AUDIO_S16,
                    xut_frequency);
    if (0 != xit_err)
    {
        printf("audio_capture.open(-1, [%d], [%d], [%d]) return error code: %d\n",
               xut_channels,
               X_AUDIO_S16,
               xut_frequency,
               xit_err);
        fwave_wpcm.close();
        return -1;
    }

    printf("capture:\n\tfrequency:%6d\n\tchannels :%6d\n\tformat   :%6d\n",
           audio_capture.audio_spec().xut_frequency,
           audio_capture.audio_spec().xut_channels,
           audio_capture.audio_spec().xut_format);

    //======================================

    system("pause");

    audio_capture.close();
    fwave_wpcm.close();

    SDL_Quit();

    //======================================

    return 0;
}
